package engine.newUi
{
	import com.greensock.TweenLite;
	import com.greensock.plugins.VolumePlugin;
	
	import engine.control.loading.MultLoader;
	import engine.data.ModelLocator;
	import engine.data.View;
	import engine.logic.event.LogicEvent;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.tip.TipsMainLogic;
	import engine.ui.MainLogic;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.button.NxButton;
	import newx.com.control.text.NxLabel;

	/**
	 * 幸运转盘逻辑处理类
	 * @author zjing
	 * 
	 */
	public class LuckyWheelLogic
	{
		private static var _self : LuckyWheelLogic;
		private var _ui : LuckyWheelUI;
		private var _model : ModelLocator;
		private var timer : Timer;
		private var speedControlTimer : Timer;
		
		/**免费次数**/
		private var FreeCount : int;
		
		/**是否免费**/
		private var isFreeTurn : Boolean;
		
		/**最大连续转动次数**/
		private const maxTurnCount : int = 20;
		
		/**转一次需要的铜钱**/
		private var GoldCoin : int;
		
		/**转一次需要的元宝**/
		private var MagicCoin : int;
		
		/**当前光标所在的索引**/
		private var currentIndex : int;
		
		/**返回中奖的索引**/
		private var prizeNum : int;
		
		/**返回的奖品列表数据**/
		private var GroupData : Array;
		
		/**是否转动的状态**/
		private var isTurn : Boolean;
		
		/**奖品tips显示**/
		private var _tips : TipsMainLogic;
		
		/**需要连续转动的次数**/
		private var needTurnCount : int;
		
		/**连续转动保存的奖品**/
		private var prizeArr : Array;
		
		/**是否连续转动**/
		private var isContinuumTurn : Boolean;
		
		/**连续转动每次转动每次的缩短时间**/
		private var TurnTime : int;
		
		/**连续转动每次转动每次的加快速度**/
		private var TurnSpeed : int;
		
		/**拾取物品特效的物品克隆MC**/
		private var TweenLiteMC : MovieClip;	
		/**
		* 0,
		* 1,白
		* 2.绿
		* 3.蓝
		* 4.紫红
		* 5.橙黄
		* 6.黄
		* 7.浅黄
		*/		
		protected const COLOR_TABLE	:Array	=	["","#ffffff'>","#1eff00'>","#20BFF8'>","#a335ee'>","#E8912E'>","#e5cc80'>","#ffff00'>"];
		
		/**下面是界面组**/
		private var uiBG : MovieClip;
		private var ExpendLab : TextField;
		private var CountLab : TextField;
		private var UseMoneyLab : TextField;
		private var TurnCountLab : TextField;
		private var CurrentCircleItem : MovieClip;
		private var SurpriseMC : MovieClip;
		private var ItemsGroup : Array;		//所有圆圈item
		private var CountDownBtn : NxButton;
		private var CountUpBtn : NxButton;
		
		private var StopBtn : NxButton;
		private var StartBtn : NxButton;
		private var CloseBtn : NxButton;

		public function LuckyWheelLogic()
		{
		}
		
		public function Init():void
		{
			_model = ModelLocator.getInstance();
			_tips = TipsMainLogic.instance;
			timer = new Timer(50);
			speedControlTimer = new Timer(500);
			
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_LUCKTURNPLATE_ITEM_LIST),GetGroupList);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_LUCKTURNPLATE_RESULT),GetPrizeAndList);	
			
		}
		
		private function CreateUI():void
		{
			ui.windowBG.visible = false;
			uiBG = MultLoader.getInstance().getSourceByUrl(StageInfo.swfResPath+"LuckWheel.swf");
			uiBG.y = -80;
			UseMoneyLab = uiBG["UseMoneyLab"];
			ExpendLab = uiBG["ExpendLab"];
			CountLab = uiBG["CountLab"];
			TurnCountLab = uiBG["TurnCountLab"];
			CurrentCircleItem = uiBG["CurrentCircleItem"];
			SurpriseMC = uiBG["SurpriseMC"];
			ItemsGroup = new Array;
			for(var i:int = 0;i<20;i++)
			{
				ItemsGroup.push(uiBG["item_"+i.toString()]);
			}
			
			CountUpBtn = new NxButton();
			CountUpBtn.skin = "Button_103";
			CountUpBtn.x = 310;
			CountUpBtn.y = 362;
			CountUpBtn.width = 29;
			CountUpBtn.height = 10;
			CountUpBtn.label = "";
			CountDownBtn = new NxButton();
			CountDownBtn.skin = "Button_104";
			CountDownBtn.x = 310;
			CountDownBtn.y = CountUpBtn.y+CountUpBtn.height;
			CountDownBtn.width = 29;
			CountDownBtn.height = 10;
			CountDownBtn.label = "";
			
			StartBtn = new NxButton();
			StartBtn.skin = "Button_100";
			StartBtn.x = 199;
			StartBtn.y = 203;
			StartBtn.width = 122;
			StartBtn.height = 121;
			StartBtn.label = "";
			
			StopBtn = new NxButton();
			StopBtn.skin = "Button_101";
			StopBtn.x = 199;
			StopBtn.y = 203;
			StopBtn.width = 122;
			StopBtn.height = 121;		
			StopBtn.label = "";

			CloseBtn = new NxButton();
			CloseBtn.skin = "Button_102";
			CloseBtn.x = 475;
			CloseBtn.y = 118;
			CloseBtn.width = 46;
			CloseBtn.height = 49;		
			CloseBtn.label = "";
			
			uiBG.addChild(CountUpBtn);
			uiBG.addChild(CountDownBtn);
			uiBG.addChild(StopBtn);
			uiBG.addChild(StartBtn);
			uiBG.addChild(CloseBtn);
			
			CountLab.text = StaticIni.getSwfLangStr("UIString_1000580");
			needTurnCount = 1;
			TurnCountLab.text = String(needTurnCount);
			CurrentCircleItem.gotoAndStop(1);
			SurpriseMC.gotoAndStop(7);
			SurpriseMC.visible = false;
			
			ui.addChild(uiBG);
			addAllListener();
		}
		
		/**
		 *向服务端请求查询奖励物品 
		 * 
		 */
		private function queryGroupList():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_LUCKTURNPLATE_INFO);
		}
		
		/**
		 *设置需要消耗的铜钱显示 
		 * @param GoldCoin
		 * @param MagicCoin
		 * 
		 */
		private function SetFreeOrNeedMoney(GoldCoin:int,MagicCoin:int):void
		{
			if(FreeCount>0)//免费次数
			{
				isFreeTurn = true;
				ExpendLab.text = StaticIni.getSwfLangStr("UIString_1000581");
				UseMoneyLab.text = FreeCount.toString();
			}
			else
			{
				isFreeTurn = false;
				if(GoldCoin>0)	
				{
					ExpendLab.text = StaticIni.getSwfLangStr("UIString_1000578");
					UseMoneyLab.text = (GoldCoin*needTurnCount).toString();
				}
				else
				{
					ExpendLab.text = StaticIni.getSwfLangStr("UIString_1000579");
					UseMoneyLab.text = (MagicCoin*needTurnCount).toString();
				}
			}
			isHaveEnoughMoneyToTrun();
		}
		
		/**
		 *收到奖品列表数据
		 * @param event
		 * 
		 */
		private function GetGroupList(event:CustomMsgEvent):void
		{
			var p:int;
			FreeCount = event.pList[p++];
			GoldCoin = event.pList[p++];
			MagicCoin = event.pList[p++];
			var listCount : int = event.pList[p++];
			GroupData = new Array;
			for(var i:int = 0;i<listCount;i++)
			{
				var itemData : Object = new Object;
				itemData["configID"] = event.pList[p++];
				itemData["modelID"]  = StaticIni.getIniVar(itemData["configID"],"LuckyImage");
				itemData["itemCount"] = event.pList[p++];
				GroupData.push(itemData);
			}
			UpdataGroup(GroupData);
			SetFreeOrNeedMoney(GoldCoin,MagicCoin);
			isHaveEnoughMoneyToTrun();
		}
		
		/**
		 *收到中奖 和 奖品列表数据 
		 * @param event
		 * 
		 */
		private function GetPrizeAndList(event:CustomMsgEvent):void
		{
			if(event.pList.length == 0)
			{
				afterTrun();
				return;
			}
			var p:int;
			FreeCount = event.pList[p++];
			var resultIndex : int = event.pList[p++];
			if(prizeArr!=null && GroupData!=null)
			{
				prizeArr.push(GroupData[resultIndex]);
			}
			
			GoldCoin = event.pList[p++];
			MagicCoin = event.pList[p++];
			var listCount : int = event.pList[p++];
			
			GroupData = [];
			for(var i:int = 0;i<listCount;i++)
			{
				var itemData : Object = new Object;
				itemData["configID"] = event.pList[p++];
				itemData["modelID"]  = StaticIni.getIniVar(itemData["configID"],"LuckyImage");
				itemData["itemCount"] = event.pList[p++];
				GroupData.push(itemData);
			}
			turn(resultIndex);
			isHaveEnoughMoneyToTrun();
		}
		
		/**
		 *设置光标位置 
		 * 
		 */
		private function upDataCurrentItem():void
		{
			if(currentIndex == 0 || currentIndex == 5 || currentIndex == 10 || currentIndex == 15)
			{
				CurrentCircleItem.gotoAndStop(1);
			}
			else
			{
				CurrentCircleItem.gotoAndStop(2);
			}
			CurrentCircleItem .x = ItemsGroup[currentIndex].x;
			CurrentCircleItem .y = ItemsGroup[currentIndex].y;
		}
		
		/**
		 *刷新抽奖物品显示
		 * @param porpsData
		 * 
		 */
		public function UpdataGroup(porpsData:Array):void
		{
			for(var j:int = 0;j<ItemsGroup.length;j++)
			{
				var item : MovieClip = ItemsGroup[j] as MovieClip;
				var itemData : Object = porpsData[j];
				item.bg = new NxClip();
				item.bg.isCache = true;
				item.lable	=	new NxLabel();
				item.lable.color=0xffffff;
				item.lable.glowColor = 0x081f22;
				if(j == 0||j==5||j==10||j==15)
				{
					item.bg.source = ResManager.newResClass(itemData["modelID"]+"_big");
					item.lable.x = 13;
					item.lable.y = -33;
				}
				else
				{
					item.bg.source = ResManager.newResClass(itemData["modelID"]+"_little");
					item.lable.x = 5;
					item.lable.y = -25;
				}
				
				item.lable.text = itemData["itemCount"];
				item["configid"] = itemData["configID"];
				for(var i:int =0;i<item["icon"].numChildren;i++)
				{
					item["icon"].removeChildAt(i);
				}
				item["icon"].addChild(item.bg);
				item.index		=		j;
				item["icon"].addChild(item.lable);
			}
		}
		
		
		/**
		 *显示Tips 
		 * @param event
		 * 
		 */
		private function onRollOver(event:Event):void
		{
			var cell : MovieClip = event.currentTarget as MovieClip;
			_tips.showMain(cell["configid"]);
		}
		
		/**
		 *隐藏tips 
		 * @param event
		 * 
		 */
		private function onRollOut(event:Event):void
		{
			_tips.hide();
		}
		
		/**
		 *转动次数加一
		 * @param event
		 * 
		 */
		private function onCountUpBtn(event:MouseEvent):void
		{
//			maxTurnCount = _model.scene.Role.GetProperties("PlayLuckTurnPlateCounter");
			if(int(TurnCountLab.text)>=maxTurnCount)
			{
				TurnCountLab.text = maxTurnCount.toString();
			}
			else
			{
				var trunCount : int = int(TurnCountLab.text)+1;
				TurnCountLab.text = String(trunCount);
			}
			needTurnCount = int(TurnCountLab.text);
			isHaveEnoughMoneyToTrun();
			SetFreeOrNeedMoney(GoldCoin,MagicCoin);
		}
		
		/**
		 *转动次数减一 
		 * @param event
		 * 
		 */
		private function onCountDownBtn(event:MouseEvent):void
		{
			if(int(TurnCountLab.text)<=0)
			{
				TurnCountLab.text = "0";
			}
			else
			{
				TurnCountLab.text = String(int(TurnCountLab.text)-1);
			}
			needTurnCount = int(TurnCountLab.text);
			isHaveEnoughMoneyToTrun();
			SetFreeOrNeedMoney(GoldCoin,MagicCoin);
		}
		
		/**
		 *判断有没有足够的钱开启转盘 
		 * @return 
		 * 
		 */
		private function isHaveEnoughMoneyToTrun():Boolean
		{
			var roleGoldCoin : int = _model.scene.Role.getGoldCoin();
			var roleMagicCoin : int = _model.scene.Role.getMagicCoin();
			if(roleGoldCoin>=needTurnCount*GoldCoin && roleMagicCoin>=needTurnCount*MagicCoin)
			{	
				StartBtn.enabled = true;
				StartBtn.mouseEnabled = true;
				StopBtn.enabled = true;
				StopBtn.mouseEnabled = true;
				return true;
			}
			else if(isFreeTurn)
			{
				StartBtn.enabled = true;
				StartBtn.mouseEnabled = true;
				StopBtn.enabled = true;
				StopBtn.mouseEnabled = true;
				return true;
			}
			else
			{
				StartBtn.enabled = false;
				StartBtn.mouseEnabled = false;
				StopBtn.enabled = false;
				StopBtn.mouseEnabled = false;
				return false;
			}
		}
		
		/**
		 *定时器启动，开始转动 
		 * 
		 */
		private function turn(prize:int):void
		{
			isTurn = true;
			CountDownBtn.enabled = false;
			CountUpBtn.enabled = false;
			CountDownBtn.mouseEnabled = false;
			CountUpBtn.mouseEnabled = false;
			StartBtn.visible = false;
			StopBtn.visible = true;
			prizeNum = prize;
			TurnTime = isContinuumTurn?2:0;
			TurnSpeed = isContinuumTurn?300:0;
			
			timer.reset();
			speedControlTimer.reset();
			timer.delay = 50;
			timer.start();
			speedControlTimer.start();
		}
		
		/**
		 *关闭窗口 
		 * @param event
		 * 
		 */
		private function onCloseWindow(event : MouseEvent):void
		{
			ui.showOrHide();
		}
		
		/**
		 *点击开始按钮 
		 * @param event
		 * 
		 */
		private function onStartTrun(event:MouseEvent):void
		{
			if(!isTurn && needTurnCount>0)
			{
				if(!isFreeTurn)
				{
					SitAlerLogic.getIntatic().setUIinfo("UIString_1000593",[needTurnCount*MagicCoin],startTurn,StartBtn);
				}
				else
				{
					if(FreeCount>0)
					{
						var freeMoney : int = ((needTurnCount-FreeCount)>0) ? needTurnCount-FreeCount : 0;
						SitAlerLogic.getIntatic().setUIinfo("UIString_1000593",[freeMoney*MagicCoin],startTurn,StartBtn);
					}
					else
					{
						startTurn();
					}
				}
			}
		}
		
		private function startTurn():void
		{
			isTurn = true;
			prizeArr = null;
			prizeArr = new Array();
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_PLAY_LUCKTURNPLATE);
			if(needTurnCount>1)
			{
				isContinuumTurn = true;
			}
			else
			{
				isContinuumTurn = false;
			}
		}
		
		/**
		 *终止连续转动，本次还会继续 
		 * @param event
		 * 
		 */
		private function onStopTurn(event:MouseEvent):void
		{
			if(needTurnCount>1)
			{
				needTurnCount = 1;
				StopBtn.enabled = false;
				StopBtn.mouseEnabled = false;
			}
		}
		
		/**
		 *变换光标位置的定时器 
		 * @param event
		 * 
		 */
		private function onTimer(event:TimerEvent):void
		{
			var slowIndex:int = prizeNum-(isContinuumTurn?4:7);//最后转圈变慢的中奖前7个的索引
			if(slowIndex<0)
			{
				slowIndex = slowIndex+19;
			}
			
			if(speedControlTimer.currentCount == 4-TurnTime)//第一次减速
			{
				timer.delay = 60;
			}
			else if(speedControlTimer.currentCount == 6-TurnTime)//第2次减速
			{
				timer.delay = 90;
			}
			else if(speedControlTimer.currentCount >= 8-TurnTime && currentIndex == slowIndex)//到中奖前几个的索引时候减速
			{
				timer.delay = 400-TurnSpeed;
				speedControlTimer.stop();
			}
			
			if(currentIndex == prizeNum && timer.delay == 400-TurnSpeed)//停止转动，return,不再更新光圈位置
			{
				timer.stop();
				speedControlTimer.stop();
				var prizeMC : MovieClip = ItemsGroup[currentIndex] as MovieClip;
				TweenLiteMC = CreateTweenLiteMC(prizeMC);
				uiBG.addChild(TweenLiteMC);
				TweenLite.to(TweenLiteMC,1,{x:StartBtn.x+TweenLiteMC.width,y:StartBtn.y+TweenLiteMC.height,onComplete:tweenLiteToStartBtn});
				
				SurpriseMC.visible = true;
				uiBG.setChildIndex(SurpriseMC,uiBG.numChildren-1);
				SurpriseMC.x = ItemsGroup[currentIndex].x;
				SurpriseMC.y = ItemsGroup[currentIndex].y;
				SurpriseMC.gotoAndPlay(1);
				SurpriseMC.addFrameScript( SurpriseMC.totalFrames - 1 , stopMc );
				return;
			}
			
			if(currentIndex<ItemsGroup.length-1)
			{
				currentIndex++;
			}
			else
			{
				currentIndex = 0;
			}
			upDataCurrentItem();
		}
		
		/**
		 *请求发送全服中奖通知，随后停止转动，判断是否继续下一次转动 ，
		 * 如果突然关闭界面，则不会有通知，也不会继续判断是否转动。
		 * 
		 */
		private function stopMc():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_LUCKTURNPLATE_CUR_WIN_TIPS);
			
			needTurnCount--;
			TurnCountLab.text = needTurnCount.toString();
			
			SurpriseMC.addFrameScript(SurpriseMC.totalFrames - 1 , null);
			SurpriseMC.stop();
			SurpriseMC.visible = false;
			
			SetFreeOrNeedMoney(GoldCoin,MagicCoin);
			UpdataGroup(GroupData);
			if(needTurnCount>0)
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_PLAY_LUCKTURNPLATE);
			}
			else
			{
				afterTrun();
			}
		}
		
		/**
		 *转圈完全结束
		 * 
		 */
		private function afterTrun():void
		{
			isTurn = false;
			CountDownBtn.enabled = true;
			CountUpBtn.enabled = true;
			CountDownBtn.mouseEnabled = true;
			CountUpBtn.mouseEnabled = true;
			StartBtn.visible = true;
			StopBtn.visible = false;
			StopBtn.enabled = true;
			StopBtn.mouseEnabled = true;
			needTurnCount = 1;
			TurnCountLab.text = String(needTurnCount);
			SetFreeOrNeedMoney(GoldCoin,MagicCoin);
			if(prizeArr.length>0)
			{
				var listData:String = StaticIni.getSwfLangStr("UIString_1000622");
				for(var i:int = 0;i<prizeArr.length;i++)
				{
					var prize : Object = prizeArr[i];
					
					var name : String = StaticIni.getSwfLangStr(StaticIni.getIniVar(prize["configID"],"NameID"));
					var nxName : String = setStr(prize["configID"],name);
					listData = listData+nxName+"*"+prize["itemCount"]+".";
				}
				LuckyPrizeListLogic.getInstance().upDataList(listData,prizeArr.length);
				LuckyPrizeListLogic.getInstance().ui.showOrHide();
			}
		}
		
		/**
		 *设置物品超链接 
		 * @param configid 
		 * @param name
		 * @return 
		 * 
		 */
		private function setStr(configid:int,name:String):String
		{
			var color : int = int(StaticIni.getIniVar(configid,"Color"));
			var str:String	="<a href=\"event:"
				+configid +"\">"
				+ "【"+link_color(name,color)+"】"
				+"</a>";
			return str;
		}
		
		/**
		 *设置物品等级颜色显示 
		 * @param str
		 * @param color
		 * @return 
		 * 
		 */
		protected function link_color(str:String,color:int):String
		{
			if(color < 1 || color >8)
			{
				color	=	1;
			}
			return ("<font color ='"+COLOR_TABLE[color]+str+"</font>");
		}
		
		/**
		 *拾取物品特效移动到开始按钮中间 
		 * 
		 */
		private function tweenLiteToStartBtn():void
		{
			var packBtnGlobalPoint : Point = BottomIndexLogic.getInstance().PackBtnGlobalPoint;
			var packBtnLocalPoint : Point = uiBG.globalToLocal(packBtnGlobalPoint);
			TweenLite.to(TweenLiteMC,1,{x:packBtnLocalPoint.x,y:packBtnLocalPoint.y,onComplete:tweenLiteOver});
		}
		
		/**
		 * 拾取物品特效移动到背包
		 * 
		 */
		private function tweenLiteOver():void
		{
			uiBG.removeChild(TweenLiteMC);
		}
		
		private function onSpeedControlTimer(event:TimerEvent):void
		{
		}
		
		public function BeforeShow():void
		{
			if(!isTurn)
			{
				this._ui.visible = false;
				if(uiBG == null)
				{
					MultLoader.getInstance().load([StageInfo.swfResPath+"LuckWheel.swf"],true,CreateUI)
				}
				else
				{
					addAllListener();
				}
			}
		}
		
		public function AfterHide():void
		{
			if(!isTurn)
			{
				removeAllListener();
				isTurn = false;
				CountDownBtn.enabled = true;
				CountUpBtn.enabled = true;
				CountDownBtn.mouseEnabled = true;
				CountUpBtn.mouseEnabled = true;
				StartBtn.visible = true;
				StopBtn.visible = false;
				StopBtn.enabled = true;
				StopBtn.mouseEnabled = true;
				currentIndex = 0;
				upDataCurrentItem();
				needTurnCount = 1;
				TurnCountLab.text = String(needTurnCount);
				timer.stop();
				speedControlTimer.stop();
			}
			
			
		}
		
		private function addAllListener():void
		{
			this._ui.visible = true;
			queryGroupList();
			StartBtn.addEventListener(MouseEvent.CLICK,onStartTrun);
			StopBtn.addEventListener(MouseEvent.CLICK,onStopTurn);
			CloseBtn.addEventListener(MouseEvent.CLICK,onCloseWindow);
			CountUpBtn.addEventListener(MouseEvent.CLICK,onCountUpBtn);
			CountDownBtn.addEventListener(MouseEvent.CLICK,onCountDownBtn);
			for(var i:int=0;i<ItemsGroup.length;i++)
			{
				ItemsGroup[i].addEventListener(MouseEvent.ROLL_OVER,onRollOver);
				ItemsGroup[i].addEventListener(MouseEvent.ROLL_OUT,onRollOut);
			}
			
			timer.addEventListener(TimerEvent.TIMER,onTimer);
			speedControlTimer.addEventListener(TimerEvent.TIMER,onSpeedControlTimer);
			if(!SceneManager._itemIconLoad)
			{
				_model.addEventListener(LogicEvent.ITEMICONLOADED,loaded);
				function loaded():void
				{
					UpdataGroup(GroupData);
				}
			}
		}
		
		private function removeAllListener():void
		{
			StartBtn.removeEventListener(MouseEvent.CLICK,onStartTrun);
			StopBtn.removeEventListener(MouseEvent.CLICK,onStopTurn);
			CloseBtn.removeEventListener(MouseEvent.CLICK,onCloseWindow);
			CountUpBtn.removeEventListener(MouseEvent.CLICK,onCountUpBtn);
			CountDownBtn.removeEventListener(MouseEvent.CLICK,onCountDownBtn);
			for(var i:int=0;i<ItemsGroup.length;i++)
			{
				ItemsGroup[i].removeEventListener(MouseEvent.ROLL_OVER,onRollOver);
				ItemsGroup[i].removeEventListener(MouseEvent.ROLL_OUT,onRollOut);
			}
			
			timer.removeEventListener(TimerEvent.TIMER,onTimer);
			speedControlTimer.removeEventListener(TimerEvent.TIMER,onSpeedControlTimer);
		}
		
		/**
		 *画出中奖的物品MC 
		 * @param mc
		 * @return 
		 * 
		 */
		private function CreateTweenLiteMC(mc:MovieClip):MovieClip
		{
			var newItem : MovieClip = new MovieClip;
			var CricleMask : MovieClip; 
			var modelID:String = StaticIni.getIniVar(mc["configid"],"LuckyImage");
			newItem.x = mc.x;
			newItem.y = mc.y;
			newItem.bg = new NxClip();
			newItem.bg.isCache = true;
			if(mc.index == 0||mc.index==5||mc.index==10||mc.index==15)
			{
				CricleMask = DrawCricle(43);
				newItem.bg.source = ResManager.newResClass(modelID+"_big");
			}
			else
			{
				CricleMask = DrawCricle(28);
				newItem.bg.source = ResManager.newResClass(modelID+"_little");
			}
			newItem.addChild(newItem.bg);
			newItem.addChild(CricleMask);
			newItem.mask = CricleMask;
			return newItem;
		}
		
		/**
		 *画一个园型遮罩 
		 * @param radius
		 * @return 
		 * 
		 */
		private function DrawCricle(radius:Number):MovieClip
		{
			var cricle:MovieClip = new MovieClip;
			cricle.graphics.beginFill(0xffff00,0.5);
			cricle.graphics.drawCircle(0,0,radius);
			cricle.graphics.endFill();
			return cricle;
		}
		
		/**
		 *背包已满 
		 * @return 
		 * 
		 */
		public function get isPackFull():Boolean
		{
			return MainLogic.getInstance().checkViewFull(View.VIEWPORT_TOOL,"BaseCap");
		}
		
		public function get ui():LuckyWheelUI
		{
			if(!_ui)
			{
				_ui = new LuckyWheelUI;
			}
			return _ui;
		}
		
		public static function getInstance():LuckyWheelLogic
		{
			if(!_self)
			{
				_self = new LuckyWheelLogic;
			}
			return _self;
		}
	}
}